package org.xdq.demo.service.security.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.xdq.demo.dao.security.RoleDao;
import org.xdq.demo.page.PageParam;
import org.xdq.demo.service.security.RoleService;
import org.xdq.demo.util.MyUtils;
import org.xdq.demo.vo.MenuVo;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleDao roleDao;

    private int maxId;

    @PostConstruct
    public void init(){
        maxId = roleDao.findMaxId();
    }

    private synchronized int getNewId(){
        return ++maxId;
    }

    @Override
    public Map<String, Object> getRolePage(PageParam dto) {
        try {
            return MyUtils.getPage(dto,()->roleDao.findRoleList());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<MenuVo> getMenuVoList() {
        return roleDao.findMenuVoList();
    }

    @Override
    public List<Integer> getRoleMenuIdList(Integer roId) {
        return roleDao.findRoleMenuIdList(roId);
    }

    @Override
    public void setRoleModules(Map<String, Object> map) {
        roleDao.deleteRoleFun(map);
        roleDao.insertRoleFun(map);
    }

    @Override
    public void addNewRole(Map<String, Object> dto) {
        dto.put("ro_id",getNewId());
        roleDao.insertRole(dto);
    }
}
